﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FruitManagerApp
{
    public class CityEditViewModel : NotificationObject
    {
        private CityEdit gWindow;
        private CityViewModel gCityViewModel;
        private CCity gCity;

        public CityEditViewModel(CityEdit pWindow, CityViewModel pCityViewModel, CCity pCity)
        {
            gWindow = pWindow;
            gCityViewModel = pCityViewModel;
            gCity = pCity;

            Initialize();
        }

        public void Initialize()
        {
            HttpResponse response = API.SelectProvince();
            if (response.S == HttpResponse.Success)
            {
                Province = JSON.parse<ObservableCollection<CProvince>>(response.D);
            }

            UID = gCity.UID;
            Name = gCity.Name;
            ProvinceUID = gCity.ProvinceUID;
        }

        private int uid;
        public int UID
        {
            get
            {
                return uid;
            }

            set
            {
                uid = value;
                RaisePropertyChanged("UID");
            }
        }

        private String name;
        public String Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        private int provinceUID;
        public int ProvinceUID
        {
            get
            {
                return provinceUID;
            }

            set
            {
                provinceUID = value;
                RaisePropertyChanged("ProvinceUID");
            }
        }

        private ObservableCollection<CProvince> province;
        public ObservableCollection<CProvince> Province
        {
            get
            {
                return province;
            }

            set
            {
                province = value;
                RaisePropertyChanged("Province");
            }
        }

        protected DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                return saveCommand ?? (saveCommand = new DelegateCommand<object>(ExcuteSaveCommand));
            }
        }

        protected virtual void ExcuteSaveCommand(object obj)
        {
            API.UpdateCity(new CCity(UID, Name, ProvinceUID), UID);
            gCityViewModel.UpdateList();
            gWindow.Close();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(ExcuteQuitCommand));
            }
        }

        protected virtual void ExcuteQuitCommand(object obj)
        {
            gWindow.Close();
        }
    }
}
